package com.formiaomiao.demain.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.adapter.WeatherAdapter;
import com.formiaomiao.demain.base.BaseActivity;
import com.formiaomiao.demain.model.WeatherBean;
import com.formiaomiao.demain.service.LocationService;
import com.formiaomiao.demain.util.TimeUtils;
import com.formiaomiao.demain.weight.LoadingDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class WeatherActivity extends BaseActivity {

    @BindView(R.id.iv_class_back)
    ImageView ivClassBack;
    @BindView(R.id.tv_class_name)
    TextView tvClassName;
    @BindView(R.id.tv_weather)
    TextView tvWeather;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_temperature)
    TextView tvTemperature;
    @BindView(R.id.rl_weather)
    RecyclerView rlWeather;
    @BindView(R.id.ll_weather)
    LinearLayout llWeather;

    WeatherBean weatherBean = new WeatherBean();
    List<WeatherBean.ResultBean.FutureBean> list = new ArrayList<>();
    WeatherAdapter adapter;

    LoadingDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);
        ButterKnife.bind(this);
        ViewInit();
    }

    public void ViewInit(){
        dialog =new LoadingDialog(this);
        tvClassName.setText("天气");
        if(TimeUtils.getCurrentTime()){
            llWeather.setBackgroundResource(R.mipmap.light);
        }else{
            llWeather.setBackgroundResource(R.mipmap.day);
        }
        adapter = new WeatherAdapter(list,WeatherActivity.this);
        rlWeather.setLayoutManager(new LinearLayoutManager(this));
        rlWeather.setAdapter(adapter);
        DataInit();
    }

    public void DataInit(){
        dialog.show();
        HttpParams params = new HttpParams();
        params.put("key","246634d11162f");
        params.put("city", LocationService.city);
        params.put("province", LocationService.province);
        OkGo.<String>post("http://apicloud.mob.com/v1/weather/query").params(params).tag(this).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                System.out.println(response);
                try {
                    weatherBean = gson.fromJson(response.body(),WeatherBean.class);
                    list.addAll(weatherBean.getResult().get(0).getFuture());
                    adapter.notifyDataSetChanged();
                    tvAddress.setText(LocationService.province+","+LocationService.city);
                    tvWeather.setText(weatherBean.getResult().get(0).getWeather());
                    tvTemperature.setText(weatherBean.getResult().get(0).getTemperature());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                dialog.dismiss();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dialog.dismiss();
            }
        });
    }

    @OnClick(R.id.iv_class_back)
    public void onViewClicked() {
        finish();
    }
}
